package com.example.constellation.starfrag;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.constellation.R;
import com.example.constellation.bean.StarInfoBean;
import com.example.constellation.util.AssetsUtil;
import de.hdodenhof.circleimageview.CircleImageView;

import java.util.List;
import java.util.Map;

public class StarBaseAdapter extends BaseAdapter {
    Context context;
    List<StarInfoBean.StarinfoBean> mDatas;
    Map<String, Bitmap> logoMap;
    public StarBaseAdapter(Context context, List<StarInfoBean.StarinfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        logoMap= AssetsUtil.getLogoImgMap();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_star_gv,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder=(ViewHolder) view.getTag();
        }
        //获取指定位置的数据
        StarInfoBean.StarinfoBean bean=mDatas.get(i);
        holder.tv.setText(bean.getName());
        //根据名称在内存当中进行查找获取图片的名称
        String logoname=bean.getLogoname();
        Bitmap bitmap=logoMap.get(logoname);
        holder.iv.setImageBitmap(bitmap);
        return view;
    }
    class ViewHolder{
        CircleImageView iv;
        TextView tv;
        public ViewHolder(View view){
            iv=view.findViewById(R.id.item_star_iv);
            tv=view.findViewById(R.id.item_star_tv);
        }
    }
}
